package me.iwf.photopicker;

import static android.widget.Toast.LENGTH_LONG;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.entity.Photo;
import me.iwf.photopicker.event.OnItemCheckListener;
import me.iwf.photopicker.fragment.ImagePagerFragment;
import me.iwf.photopicker.fragment.PhotoPickerFragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class PhotoPickerActivity extends AppCompatActivity {

	private PhotoPickerFragment pickerFragment;
	private ImagePagerFragment imagePagerFragment;

	public final static String EXTRA_MAX_COUNT = "MAX_COUNT";
	public final static String EXTRA_SHOW_CAMERA = "SHOW_CAMERA";
	public final static String EXTRA_SHOW_GIF = "SHOW_GIF";
	public final static String KEY_SELECTED_PHOTOS = "SELECTED_PHOTOS";

	private MenuItem menuDoneItem;

	public final static int DEFAULT_MAX_COUNT = 9;

	private int maxCount = DEFAULT_MAX_COUNT;

	/** to prevent multiple calls to inflate menu */
	private boolean menuIsInflated = false;

	private boolean showGif = false;
	
	protected TextView titleText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		boolean showCamera = getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA, true);
		boolean showGif = getIntent().getBooleanExtra(EXTRA_SHOW_GIF, false);
		setShowGif(showGif);

		setContentView(R.layout.activity_photo_picker);

		Toolbar mToolbar = (Toolbar) findViewById(R.id.id_common_toolbar);
		
		//设置标题
		mToolbar.setTitle("");
		titleText = (TextView) mToolbar.findViewById(R.id.id_common_bar_title);
		titleText.setText(R.string.images);

		//使得ToolBar实现
		setSupportActionBar(mToolbar);

		ActionBar actionBar = getSupportActionBar();

		assert actionBar != null;

		//设置后退按钮（自定义）
		actionBar.setDisplayHomeAsUpEnabled(true);// 给左上角图标的左边加上一个返回的图标 
		mToolbar.setNavigationIcon(R.drawable.sl_common_top_title_back);
		mToolbar.invalidate();
		mToolbar.setNavigationOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				onBackPressed();
			}
		});

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			actionBar.setElevation(25);
		}

		maxCount = getIntent().getIntExtra(EXTRA_MAX_COUNT, DEFAULT_MAX_COUNT);

		pickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPickerFragment);

		pickerFragment.getPhotoGridAdapter().setShowCamera(showCamera);

		pickerFragment.getPhotoGridAdapter().setOnItemCheckListener(new OnItemCheckListener() {
			@Override
			public boolean OnItemCheck(int position, Photo photo, final boolean isCheck, int selectedItemCount) {

				int total = selectedItemCount + (isCheck ? -1 : 1);

				menuDoneItem.setEnabled(total > 0);

				if (maxCount <= 1) {
					List<Photo> photos = pickerFragment.getPhotoGridAdapter().getSelectedPhotos();
					if (!photos.contains(photo)) {
						photos.clear();
						pickerFragment.getPhotoGridAdapter().notifyDataSetChanged();
					}
					return true;
				}

				if (total > maxCount) {
					Toast.makeText(getActivity(), getString(R.string.over_max_count_tips, maxCount), LENGTH_LONG).show();
					return false;
				}
				menuDoneItem.setTitle(getString(R.string.done_with_count, total, maxCount));
				return true;
			}
		});

	}

	/**
	 * Overriding this method allows us to run our exit animation first, then exiting
	 * the activity when it complete.
	 */
	@Override
	public void onBackPressed() {
		if (imagePagerFragment != null && imagePagerFragment.isVisible()) {
			imagePagerFragment.runExitAnimation(new Runnable() {
				public void run() {
					if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
						getSupportFragmentManager().popBackStack();
					}
				}
			});
		} else {
			super.onBackPressed();
		}
	}

	public void addImagePagerFragment(ImagePagerFragment imagePagerFragment) {
		this.imagePagerFragment = imagePagerFragment;
		getSupportFragmentManager().beginTransaction().replace(R.id.container, this.imagePagerFragment).addToBackStack(null).commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!menuIsInflated) {
			getMenuInflater().inflate(R.menu.menu_picker, menu);
			menuDoneItem = menu.findItem(R.id.done);
			menuDoneItem.setEnabled(false);
			menuIsInflated = true;
			return true;
		}
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			super.onBackPressed();
			return true;
		}

		if (item.getItemId() == R.id.done) {
			Intent intent = new Intent();
			ArrayList<String> selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths();
			intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos);
			setResult(RESULT_OK, intent);
			finish();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public PhotoPickerActivity getActivity() {
		return this;
	}

	public boolean isShowGif() {
		return showGif;
	}

	public void setShowGif(boolean showGif) {
		this.showGif = showGif;
	}
}
